﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Communergy.Common;
using CommunergyUploader;

namespace CurrentCostGUI
{
    public partial class AddNewMeter : Form
    {
        public AddNewMeter()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void txtNewMeterName_TextChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = txtNewMeterName.Text.Length > 0;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            Communergy.Common.Meter newMeter = new Communergy.Common.Meter();
            newMeter.MeterName = txtNewMeterName.Text;
            IMeterRepository localMetersRepository = CommunergyClientFactory.GetLocalMeterStore();
            var savedMeter = localMetersRepository.CreateNewMeter(newMeter);

            //TODO move this logic to main form - make available as property.
            var meterForm = new Meter();
            meterForm.MdiParent = this.MdiParent;
            meterForm.BindMeterToForm(savedMeter);
            meterForm.Show();

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
